<?php

namespace apiend\modules\shop\models;

use Yii;
use common\helpers\ArrayHelper;
use common\helpers\Url;
use common\models\shop\ShopAttachment;
use apiend\modules\shop\models\ShopGoodsSales;

/**
 * 商品详情模型
 * 
 * @author lisen<lisen@163.com>
 * @since 2.0
 */
class ShopProductDetail extends \common\models\shop\ShopProduct {

    public $product_id;

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return ArrayHelper::merge(parent::rules(), [
            [['product_id'], 'required'],
        ]);
    }

    public function getGoodsStock() {
        return $this->hasMany(ShopGoodsSales::className(), ['product_id' => 'id']);
    }

    public function getStock() {
        return $this->getGoodsStock()->sum('shelf + stock - total');
    }

    public function getGoodsSales() {
        return $this->hasMany(ShopGoodsSales::className(), ['product_id' => 'id']);
    }

    public function getSales() {
        return $this->getGoodsStock()->sum('total - stock');
    }

    public function getAlbum() {
        return $this->hasMany(ShopAttachment::className(), ['product_id' => 'id'])
        ->orderBy(['listorder' => SORT_ASC]);
    }

    /**
     * {@inheritdoc}
     */
    public function search() {
        return static::findOne(['id' => $this->product_id]);
    }

}
